package com.atguigu.bigdata.edu.realtime.app.dws;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.bigdata.edu.realtime.app.BaseAppV1;
import com.atguigu.bigdata.edu.realtime.bean.TradeOrderBean;
import com.atguigu.bigdata.edu.realtime.common.Constant;
import com.atguigu.bigdata.edu.realtime.util.AtguiguUtil;
import com.atguigu.bigdata.edu.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichFilterFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.HashSet;

/**
 * 从 Kafka 订单明细主题读取数据，统计当日下单独立用户数和新增下单用户数，封装为实体类，写入 ClickHouse。
 */
public class Dws_08_DwsTradeOrderWindow extends BaseAppV1 {

    public static void main(String[] args) {
        new Dws_08_DwsTradeOrderWindow().init(4008, 2, "Dws_08_DwsTradeOrderWindow", Constant.TOPIC_DWD_TRADE_ORDER_DETAIL);
    }

    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> streamSource) {
        //去重
        SingleOutputStreamOperator<JSONObject> distinctedStream = distinctByOrderDetailId(streamSource);


        distinctedStream
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((obj, ts) -> obj.getLong("ts") * 1000)
                )
                .keyBy(obj -> obj.getString("user_id"))
                .process(new KeyedProcessFunction<String, JSONObject, TradeOrderBean>() {

                    private ValueState<String> lastOrderDateState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        lastOrderDateState = getRuntimeContext().getState(new ValueStateDescriptor<String>("lastOrderDateState", String.class));
                    }

                    @Override
                    public void processElement(JSONObject value, Context ctx, Collector<TradeOrderBean> out) throws Exception {
                        String lastOrderDate = lastOrderDateState.value();
                        Long ts = value.getLong("ts");
                        String sourceSite = value.getString("source_site");
                        BigDecimal originAmount = value.getBigDecimal("origin_amount");
                        String today = AtguiguUtil.toDate(ts);
                        HashSet<String> orderIdSet = new HashSet<>();
                        orderIdSet.add(value.getString("order_id"));

                        long ouuC = 0L;
                        long onuC = 0L;

                        if (!today.equals(lastOrderDate)) {
                            lastOrderDateState.update(today);
                            ouuC = 1L;
                            if (lastOrderDate == null){
                                onuC = 1L;
                            }
                        }

                        if (ouuC == 1){
                            TradeOrderBean bean = new TradeOrderBean("", "",sourceSite,originAmount, orderIdSet,0L,ouuC, onuC, ts);
                            out.collect(bean);
                        }
                    }
                })
                .keyBy(obj -> obj.getSourceSite())
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<TradeOrderBean>() {
                    @Override
                    public TradeOrderBean reduce(TradeOrderBean bean1, TradeOrderBean bean2) throws Exception {
                        bean1.setOrderUniqueUserCount(bean1.getOrderUniqueUserCount() + bean2.getOrderUniqueUserCount());
                        bean1.setOrderNewUserCount(bean1.getOrderNewUserCount() + bean2.getOrderNewUserCount());
                        bean1.setOriginAmount(bean1.getOriginAmount().add(bean2.getOriginAmount()));
                        bean1.getOrderIdSet().addAll(bean2.getOrderIdSet());
                        return bean1;
                    }
                }, new ProcessWindowFunction<TradeOrderBean, TradeOrderBean, String, TimeWindow>() {
                    @Override
                    public void process(String s, Context ctx, Iterable<TradeOrderBean> elements, Collector<TradeOrderBean> out) throws Exception {
                        TradeOrderBean bean = elements.iterator().next();
                        bean.setStt(AtguiguUtil.toDateTime(ctx.window().getStart()));
                        bean.setEdt(AtguiguUtil.toDateTime(ctx.window().getEnd()));

                        bean.setOrderCount((long) bean.getOrderIdSet().size());
                        bean.setTs(System.currentTimeMillis());
                        out.collect(bean);
                    }
                })
               .addSink(FlinkSinkUtil.getClickHouseSink("dws_trade_order_window", TradeOrderBean.class));
    }

    private SingleOutputStreamOperator<JSONObject> distinctByOrderDetailId(DataStreamSource<String> streamSource) {
        return streamSource
                .map(JSONObject::parseObject)
                .keyBy(obj -> obj.getString("id"))
                .filter(new RichFilterFunction<JSONObject>() {

                    private ValueState<Boolean> isFirstState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        isFirstState = getRuntimeContext().getState(new ValueStateDescriptor<Boolean>("isFirstState", Boolean.class));
                    }

                    @Override
                    public boolean filter(JSONObject value) throws Exception {
                        if (isFirstState.value() == null) {
                            isFirstState.update(false);
                            return true;
                        }
                        return false;
                    }
                });
    }
}
